import time
from openmind_hub import create_repo, delete_repo, repo_info, space_info, list_spaces, upload_file, restart_space
import unittest
from openmind_hub import  RepositoryNotFoundError, GatedRepoError
from openmind_hub.plugins.openmind.utils._error import BadRequestError
from data import license_data
from ddt import ddt, data

token = '123456789()'
repo_id = "guoxiaozhen2/space_test2014"
license = "mit"
space_sdk = "Gradio"
space_hardware = "CPU basic 2 vCPU · 16GB · FREE"
space_image = "openeuler-python3.9-mindspore2.3.1-openmind0.8.0"

@ddt
class TestOpenmindHub(unittest.TestCase):
    def test_01_create_spacerepo_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            a = repo_info(
                repo_id=repo_id,
                token=token,
                revision="main",
                repo_type="space",
                timeout=None,
                files_metadata=False
                )
            self.assertIsNotNone(a,"仓库不存在，创建失败")
        except:
            raise Exception("创建仓库有报错")
        else:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
    def test_02_create_spacerepo_duplicate_existok(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=True,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
        except:
            raise Exception("exist_ok=True未生效")
        else:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
    def test_03_create_spacerepo_duplicate_existnotok(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            raise Exception("exist_ok=False未生效")
        except BadRequestError:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
    def test_04_delete_spacerepo_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            time.sleep(3)
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
            repo_info(
                repo_id=repo_id,
                token=token,
                revision="main",
                repo_type="space",
                timeout=None,
                files_metadata=False
            )
            raise Exception("删除仓库有报错")
        except RepositoryNotFoundError:
            pass
    def test_05_delete_spacerepo_duplicate_missok(self):
        try:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
        except:
            raise Exception("missing_ok=True未生效")
    def test_06_delete_spacerepo_duplicate_missnotok(self):
        try:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=False
            )
            raise Exception("missing_ok=False未生效")
        except RepositoryNotFoundError:
            pass
    def test_07_create_spacerepo_wrong_token(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token+"A",
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            raise Exception("未校验token")
        except GatedRepoError:
            pass
    def test_08_create_spacerepo_wrong_license(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license+"2",
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            raise Exception("license未校验")
        except BadRequestError:
            pass
    def test_09_space_info_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            a = space_info(
                repo_id=repo_id,
                timeout=None,
                token=token,
            )
            self.assertIsNotNone(a, "仓库不存在，创建失败")
        except:
            raise Exception("获取数据库信息有报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
    def test_10_space_info_noexist_ropo(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            space_info(
                repo_id=repo_id + "a",
                timeout=None,
                token=token,
            )
            raise Exception("repo未校验")
        except RepositoryNotFoundError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
    def test_11_space_info_wrong_token(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            space_info(
                repo_id=repo_id,
                timeout=None,
                token=token + "a",
            )
            raise Exception("token未校验")
        except GatedRepoError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
    def test_12_list_spaces_success(self):
        author = "guoxiaozhen2"
        try:
            a = list_spaces(
                author=author,
                full=None,
                token=token,
            )
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            b = list_spaces(
                    author=author,
                    full=None,
                    token=token,
                )
            self.assertNotEqual(a, b,"未获取到列表资源")
        except:
            raise Exception("获取空间列表有报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
    def test_13_list_spaces_wrongtoken(self):
        author = "guoxiaozhen2"
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            list(list_spaces(
                author=author,
                full=None,
                token=token+"a",
            ))
            raise Exception("未校验token")
        except GatedRepoError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
    def test_14_restart_space_success(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            time.sleep(3)
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/requirements.txt",
                path_in_repo="requirements.txt",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/app.py",
                path_in_repo="app.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            time.sleep(480)
            restart_space(
                repo_id=repo_id,
                token=token
            )
        except:
            raise Exception("重启空间有报错")
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )
    def test_15_restart_space_statusisnotserving(self):
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/requirements.txt",
                path_in_repo="requirements.txt",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            upload_file(
                path_or_fileobj=r"/openmind-hub/auto_test/autotest/test_openmind_hub/app.py",
                path_in_repo="app.py",
                repo_id=repo_id,
                token=token,
                revision="main",
                commit_message="upload file",
                commit_description=None,
            )
            time.sleep(5)
            restart_space(
                repo_id=repo_id,
                token=token
            )
            raise Exception("重启空间有报错")
        except BadRequestError:
            pass
        finally:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )

    @data(*license_data.values())
    def test_16_create_spacerepo_morelicenses(self,data):
        license = data["license"]
        print(license)
        try:
            create_repo(
                repo_id=repo_id,
                private=True,
                desc="描述内容",
                license=license,
                token=token,
                repo_type="space",
                exist_ok=False,
                fullname=None,
                space_sdk=space_sdk,
                space_hardware=space_hardware,
                space_image=space_image,
            )
            a = repo_info(
                repo_id=repo_id,
                token=token,
                revision="main",
                repo_type="space",
                timeout=None,
                files_metadata=False
                )
            self.assertIsNotNone(a,"仓库不存在，创建失败")
        except:
            raise Exception("创建仓库有报错")
        else:
            delete_repo(
                repo_id=repo_id,
                token=token,
                repo_type="space",
                missing_ok=True
            )



if __name__ == '__main__':
    unittest.main()